package com.ruoyi.weigh.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.connect.SerialPortUtils;
import com.ruoyi.order.domain.SysProd;
import com.ruoyi.order.service.ISysProdService;
import com.ruoyi.receiving.domain.SysPeelingRecord;
import com.ruoyi.receiving.service.ISysPeelingRecordService;
import com.ruoyi.weigh.domain.SysWeigh;
import com.ruoyi.weigh.service.ISysWeighService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 称重记录Controller
 * 
 * @author yj
 * @date 2023-11-21
 */
@RestController
@RequestMapping("/system/weigh")
public class SysWeighController extends BaseController
{
    @Autowired
    private ISysWeighService sysWeighService;
    @Autowired
    private ISysProdService sysProdService;
    @Autowired
    private ISysPeelingRecordService sysPeelingRecordService;
    /**
     * 查询称重记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:weigh:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysWeigh sysWeigh)
    {
        startPage();
        List<SysWeigh> list = sysWeighService.selectSysWeighList(sysWeigh);
        return getDataTable(list);
    }

    /**
     * 导出称重记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:weigh:export')")
    @Log(title = "称重记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysWeigh sysWeigh)
    {
        List<SysWeigh> list = sysWeighService.selectSysWeighList(sysWeigh);
        ExcelUtil<SysWeigh> util = new ExcelUtil<SysWeigh>(SysWeigh.class);
        util.exportExcel(response, list, "称重记录数据");
    }

    /**
     * 获取称重记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:weigh:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysWeighService.selectSysWeighById(id));
    }

    /**
     * 新增称重记录
     */
    @PreAuthorize("@ss.hasPermi('system:weigh:add')")
    @Log(title = "称重记录", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody SysWeigh sysWeigh)
    {

        SysProd sysProd = sysProdService.selectSysProdByProdCode(sysWeigh.getOrderCode(), sysWeigh.getProdCode());
        if (sysProd.getDecorticateQuantity() == null) {
            sysProd.setRoughQuantity(new BigDecimal(0));
        }
        sysProd.setRoughQuantity(sysProd.getRoughQuantity().add(sysWeigh.getWeighQuantity()));
        sysProdService.updateSysProd(sysProd);
        sysWeigh.setCreateTime(DateUtils.getNowDate());
        sysWeigh.setReceiptType(0);
        return toAjax(sysWeighService.insertSysWeigh(sysWeigh));
    }

    @Log(title = "收货", businessType = BusinessType.INSERT)
    @PostMapping("goodsReceipt")
    @Transactional
    public AjaxResult goodsReceipt(@RequestBody SysWeigh sysWeigh)
    {

        SerialPortUtils.startConnect();
        String weight = SerialPortUtils.getDataHex();
        if (weight == null || weight.equals("")) {
            return AjaxResult.error("未获取到重量");
        }
        BigDecimal bigDecimal = new BigDecimal(20);
        sysWeigh.setWeighQuantity(bigDecimal);
        String photo = sysWeighService.hikPhoto();
        sysWeigh.setPicture(photo);
        return AjaxResult.success(sysWeigh);
    }

    @PreAuthorize("@ss.hasPermi('system:weigh:peelWeigh')")
    @Log(title = "去皮", businessType = BusinessType.INSERT)
    @PostMapping("peelWeigh")
    @Transactional
    public AjaxResult peelWeigh(@RequestBody SysWeigh sysWeigh)
    {
        SerialPortUtils.startConnect();
        String weight = SerialPortUtils.getDataHex();
        if (weight == null || weight.equals("")) {
            return AjaxResult.error("未获取到重量");
        }
        String photo = sysWeighService.hikPhoto();
        BigDecimal bigDecimal = new BigDecimal(weight);
        SysPeelingRecord sysPeelingRecord = new SysPeelingRecord();
        sysPeelingRecord.setOrderCode(sysWeigh.getOrderCode());
        sysPeelingRecord.setProdCode(sysWeigh.getProdCode());
        sysPeelingRecord.setProdName(sysWeigh.getProdName());
        sysPeelingRecord.setReceivingTime(sysWeigh.getCreateTime());
        sysPeelingRecord.setPeelingTime(DateUtils.getNowDate());
        sysPeelingRecord.setTareDeduction(bigDecimal);
        sysPeelingRecord.setPhoto(photo);
        SysProd sysProd = sysProdService.selectSysProdByProdCode(sysPeelingRecord.getOrderCode(), sysPeelingRecord.getProdCode());
        if (sysProd.getDecorticateQuantity() == null) {
            sysProd.setDecorticateQuantity(new BigDecimal(0));
        }
        sysProd.setDecorticateQuantity(sysProd.getDecorticateQuantity().add(bigDecimal));
        sysProd.setActualQuantity(sysProd.getRoughQuantity().subtract(sysProd.getDecorticateQuantity()));
        sysProdService.updateSysProd(sysProd);
        return toAjax(sysPeelingRecordService.insertSysPeelingRecord(sysPeelingRecord));
    }
    /**
     * 批量去皮
     *
     */
    @PreAuthorize("@ss.hasPermi('system:weigh:peelWeighs')")
    @Log(title = "批量去皮", businessType = BusinessType.INSERT)
    @PostMapping("peelWeighs")
    @Transactional
    public AjaxResult peelWeighs(@RequestBody List<SysWeigh> sysWeighList)
    {
        SerialPortUtils.startConnect();
        String weight = SerialPortUtils.getDataHex();
        if (weight.equals("") || weight == null) {
            return AjaxResult.error("未获取到重量");
        }
//        String weight = "1";
        BigDecimal bigDecimal = new BigDecimal(weight);
        String photo = sysWeighService.hikPhoto();
        for (SysWeigh sysWeigh : sysWeighList) {
            SysPeelingRecord sysPeelingRecord = new SysPeelingRecord();
            sysPeelingRecord.setOrderCode(sysWeigh.getOrderCode());
            sysPeelingRecord.setProdCode(sysWeigh.getProdCode());
            sysPeelingRecord.setProdName(sysWeigh.getProdName());
            sysPeelingRecord.setReceivingTime(sysWeigh.getCreateTime());
            sysPeelingRecord.setPeelingTime(DateUtils.getNowDate());
            sysPeelingRecord.setTareDeduction(bigDecimal);
            sysPeelingRecord.setPhoto(photo);
            SysProd sysProd = sysProdService.selectSysProdByProdCode(sysWeigh.getOrderCode(), sysWeigh.getProdCode());
            if (sysProd.getDecorticateQuantity() == null) {
                sysProd.setDecorticateQuantity(new BigDecimal(0));
            }
            sysProd.setDecorticateQuantity(sysProd.getDecorticateQuantity().add(bigDecimal));
            sysProd.setActualQuantity(sysProd.getRoughQuantity().subtract(sysProd.getDecorticateQuantity()));
            sysProdService.updateSysProd(sysProd);
            sysPeelingRecordService.insertSysPeelingRecord(sysPeelingRecord);
        }
        return AjaxResult.success();
    }

    /**
     * 按件收货
     */
//    @PreAuthorize("@ss.hasPermi('system:weigh:addPiece')")
    @Log(title = "按件收货", businessType = BusinessType.INSERT)
    @PostMapping("pieceReceipt")
    @Transactional
    public AjaxResult pieceReceipt(@RequestBody SysWeigh sysWeigh)
    {
        String photo = sysWeighService.hikPhoto();
        sysWeigh.setPicture(photo);
        return AjaxResult.success(sysWeigh);
    }

    @Log(title = "新增按件收货", businessType = BusinessType.INSERT)
    @PostMapping("addPiece")
    @Transactional
    public AjaxResult addPiece(@RequestBody SysWeigh sysWeigh)
    {
        System.out.println(sysWeigh);
        SysProd sysProd = sysProdService.selectSysProdByProdCode(sysWeigh.getOrderCode(), sysWeigh.getProdCode());
        if (sysProd.getRoughQuantity() == null) {
            sysProd.setRoughQuantity(new BigDecimal(0));
        }
        sysProd.setRoughQuantity(sysProd.getRoughQuantity().add(sysWeigh.getWeighQuantity()));
        sysProdService.updateSysProd(sysProd);
        sysWeigh.setCreateTime(DateUtils.getNowDate());
        sysWeigh.setReceiptType(1);
        return toAjax(sysWeighService.insertSysWeigh(sysWeigh));
    }
    /**
     * 修改称重记录
     */
    @PreAuthorize("@ss.hasPermi('system:weigh:edit')")
    @Log(title = "称重记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysWeigh sysWeigh)
    {
        return toAjax(sysWeighService.updateSysWeigh(sysWeigh));
    }

    /**
     * 删除称重记录
     */
    @PreAuthorize("@ss.hasPermi('system:weigh:remove')")
    @Log(title = "称重记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysWeighService.deleteSysWeighByIds(ids));
    }
}
